<?php

class Aluno_Model extends CI_Model{

	var $id;
	var $nome;
	var $matricula;
	var $turma_id;

	function listar_turmas(){
		$this->db->where('etapa_id', $this->input->get('etapa_id'));
		$this->db->where('escola_id', $this->input->get('escola_id'));
		
        $query = $this->db->get('turmas');
        return $query->result_array();
	}

	function salvar(){
		$this->id = $this->input->post('id');
        $this->nome = $this->input->post('nome');
        $this->matricula = $this->input->post('matricula');
        $this->turma_id = $this->input->post('turma_id');

        $message = $this->validar();
        if($message != ''){
        	echo $message;
        	exit;
        }

        if($this->id == ''){
            $this->db->insert('alunos', $this);
        } else{
            $this->db->update('alunos', $this, array('id' => $this->id));
        }

	}

	function exists($property, $table, $value){
        $query = $this->db->get_where( $table, array($property => $value) );
        $array = $query->result_array(); 
        return !empty($array);
    }

	function validar(){
		$message = '';
		if($this->nome == ''){
			$message = 'Insira o nome do Aluno';
		} else if($this->matricula == ''){
			$message = 'Insira a matricula do Aluno';
		} else if ($this->turma_id == ''){
			$message = 'Insira a matricula do Aluno';
		} else if ($this->exists('matricula', 'alunos', $this->matricula)){
			$message = 'Já existe um Aluno com esta Matricula';
		}

        return $message;

	}

	function listar(){
		if($this->input->get('nome'))
			$this->db->like('LOWER(nome)', strtolower($this->input->get('nome')));
		if($this->input->get('matricula'))
			$this->db->where('matricula', $this->input->get('matricula'));
        if($this->input->get('turma_id'))
        	$this->db->where('turma_id', $this->input->get('turma_id'));
        
        $query = $this->db->get('alunos');
        $array = $query->result_array();
        foreach ($array as $key => $value) {
        	$turma = $this->consultar_turma($array[$key]['turma_id']);
            $array[$key]['turma_nome'] = $turma['nome'];
            $array[$key]['etapa_nome'] = $this->consultar_etapa($turma['etapa_id'])['nome'];
            $array[$key]['escola_nome'] = $this->consultar_escola($turma['escola_id'])['nome'];

        }
        return $array;
	}

	function consultar_escola($id){
        $query = $this->db->get_where('escolas',  array('id' => $id));
        $array = $query->result_array();
        return $array[0];
    }

    function consultar_etapa($id){
        $query = $this->db->get_where('etapas',  array('id' => $id));
        $array = $query->result_array();
        return $array[0];
    }

    function consultar_turma($id){
        $query = $this->db->get_where('turmas',  array('id' => $id));
        $array = $query->result_array();
        return $array[0];
    }

    function delete(){
        $this->db->delete('alunos', array('id' => $this->input->post('id'))); 
    }

    function consultar_por_id(){
        $this->db->where('id', $this->input->get('id'));
        $query = $this->db->get('alunos');
        $array = $query->result_array();
        return $array[0]; 
    }

}

?>